package com.secneo.appshare.activities;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.views.KeywordsFlow;

public class SearchActivity extends CommonMenuActivity {
	private KeywordsFlow keywordsFlow;
	private List<String> keywordList;
	public static final int KEYWORD_LIST_NUMBER = 12;
	public static final int COLUMN_NUMBER = 3;
	private EditText mKeywordEditText;

	public static final int MSG_GET_OTHER_USER_ICON = 101;
	Drawable dw = null;
	String appIconUrl = "";
	private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
	private final int FP = ViewGroup.LayoutParams.FILL_PARENT;


	public Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Constants.GET_KEYWORD_FINISH:
				feedKeywordsFlow(keywordsFlow, keywordList);  
			    keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN); 
				break;
			case Constants.GO_TO_RESULT:
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("keyword", keywordList.get(msg.arg1));
				intent.putExtras(bundle);
				intent.setClass(SearchActivity.this, SearchResultActivity.class);
				SearchActivity.this.startActivity(intent);
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	private static void feedKeywordsFlow(KeywordsFlow keywordsFlow, List<String> list) {
		Random random = new Random();
		for (int i = 0; i < KeywordsFlow.MAX; i++) {
			int ran = random.nextInt(list.size());
			String tmp = list.get(ran);
			keywordsFlow.feedKeyword(tmp,ran);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.appshare_search);
		AppShareApplication.getInstance().addActivity(this);
		keywordList = new ArrayList<String>();
		mKeywordEditText = (EditText) findViewById(R.id.text_account);

		Thread updateThread = new Thread() {
			public void run() {
				try {
					RemoteApiImpl remoteApi = new RemoteApiImpl();
					int count = remoteApi.getHotKeyword(SearchActivity.this,
							KEYWORD_LIST_NUMBER, keywordList);
					if (count > 0) {
						Message msg = new Message();
						msg.what = Constants.GET_KEYWORD_FINISH;
						mHandler.sendMessage(msg);
						return;
					}

				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		};
		updateThread.start();

		Button returnButton = (Button) findViewById(R.id.img_ret);
		returnButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				/*
				 * final String uid =
				 * LocalStore.getLocaleStoreInstance(SearchActivity
				 * .this).getUid(); RemoteApiImpl remoteApi = new
				 * RemoteApiImpl(); List<AppRecom> mTalkList = new
				 * ArrayList<AppRecom>(); mTalkList =
				 * remoteApi.getFriendsList(SearchActivity.this, uid, 4);
				 */
				finish();
			}
		});

		TextView searchButton = (TextView) findViewById(R.id.img_search);
		searchButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String keyword = mKeywordEditText.getText().toString();
				if ("".equalsIgnoreCase(keyword)) {
					// LocalStore.getLocaleStoreInstance().sendRenrenNews();
					// LocalStore.getLocaleStoreInstance().getRenrenFriends();

					return;
				}

				Intent intent = new Intent();
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				Bundle bundle = new Bundle();
				bundle.putString("keyword", keyword);
				intent.putExtras(bundle);
				intent.setClass(SearchActivity.this, SearchResultActivity.class);
				SearchActivity.this.startActivity(intent);

			}
		});

		keywordsFlow = (KeywordsFlow) findViewById(R.id.TableLayoutText);
		keywordsFlow.setHandler(mHandler);
//		tableLayout.setStretchAllColumns(true);
	}
}
